package org.xinhe.example.quartz.management.controller;

import org.xinhe.example.quartz.management.common.ApiResponse;
import org.xinhe.example.quartz.management.common.PageWrapper;
import org.xinhe.example.quartz.management.manager.JobWrapper;
import org.xinhe.example.quartz.management.manager.ScheduleManager;
import org.quartz.SchedulerException;
import org.quartz.utils.Key;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Title: ScheduleController</p>
 * <p>Date: 17/1/2 </p>
 * <p>Description: </p>
 *
 * @author xinhe.sun
 */
@RestController
@RequestMapping("/api/schedule")
public class ScheduleController {

    public static final Logger LOGGER = LoggerFactory.getLogger(ScheduleController.class);

    @Autowired
    private ScheduleManager scheduleManager;

    @RequestMapping("job/list")
    public ApiResponse<PageWrapper<JobWrapper>> listJob() throws SchedulerException {
        ApiResponse<PageWrapper<JobWrapper>> response = ApiResponse.succesResponse();

        PageWrapper<JobWrapper> wrapper = new PageWrapper<>();

        //设置总数、作业列表
        wrapper.setTotal(scheduleManager.countTriggers(Key.DEFAULT_GROUP));
        wrapper.setRows(scheduleManager.getJobsByGroup(Key.DEFAULT_GROUP));

        response.setData(wrapper);


        return response;
    }

    @RequestMapping("trigger/pause")
    public ApiResponse pauseTrigger(@RequestParam("triggerName") String triggerName,
                                    @RequestParam(value = "groupName", required = false) String groupName)
            throws SchedulerException {
        LOGGER.info("暂停触发器：triggerName = {}，groupName = {}", triggerName, groupName);
        ApiResponse response = ApiResponse.succesResponse();
        scheduleManager.pauseTrigger(triggerName, groupName);
        return response;
    }

    @RequestMapping("trigger/resume")
    public ApiResponse resumeTrigger(@RequestParam("triggerName") String triggerName,
                                     @RequestParam(value = "groupName", required = false) String groupName)
            throws SchedulerException {
        LOGGER.info("恢复触发器：triggerName = {}，groupName = {}", triggerName, groupName);
        ApiResponse response = ApiResponse.succesResponse();
        scheduleManager.resumeTrigger(triggerName, groupName);
        return response;
    }

    @RequestMapping("job/pause")
    public ApiResponse pauseJob(@RequestParam("jobName") String jobName,
                                @RequestParam(value = "groupName", required = false) String groupName)
            throws SchedulerException {
        LOGGER.info("暂停作业：jobName = {}，groupName = {}", jobName, groupName);
        ApiResponse response = ApiResponse.succesResponse();
        scheduleManager.pauseJob(jobName, groupName);
        return response;
    }

    @RequestMapping("job/resume")
    public ApiResponse resumeJob(@RequestParam("jobName") String jobName,
                                 @RequestParam(value = "groupName", required = false) String groupName)
            throws SchedulerException {
        LOGGER.info("恢复作业：jobName = {}，groupName = {}", jobName, groupName);
        ApiResponse response = ApiResponse.succesResponse();
        scheduleManager.resumeJob(jobName, groupName);
        return response;
    }

    @RequestMapping("job/delete")
    public ApiResponse deleteJob(@RequestParam("jobName") String jobName,
                                 @RequestParam(value = "groupName", required = false) String groupName)
            throws SchedulerException {
        LOGGER.info("删除作业：jobName = {}，groupName = {}", jobName, groupName);
        ApiResponse response = ApiResponse.succesResponse();
        scheduleManager.deleteJob(jobName, groupName);
        return response;
    }

    @RequestMapping("job/trigger")
    public ApiResponse triggerJob(@RequestParam("jobName") String jobName,
                                  @RequestParam(value = "groupName", required = false) String groupName)
            throws SchedulerException {
        LOGGER.info("立即执行一次作业：jobName = {}，groupName = {}", jobName, groupName);
        ApiResponse response = ApiResponse.succesResponse();
        scheduleManager.triggerJob(jobName, groupName);
        return response;
    }

}
